import { _decorator, Collider, Component, ITriggerEvent, math, Node, physics, tween, v3, Vec3 } from 'cc';
import { Barrier } from './Barrier';
import { Label3D } from '../../1/Label3D';
import { ContentColor, Group } from './GameConstant';
import { GameUtil } from './GameUtil';
import { GameManager } from './GameManager';
import { PoolManager } from '../AshFramework/Managers/PoolManager';
const { ccclass, property } = _decorator;

const green: ContentColor = ContentColor.Green;
const decrease: number = -0.0004;
const plan: number[] = [0.0067, 0.0032, 0.0003];
const angle: Vec3[] = [v3(0, -90, 0), v3(-90, -90, 0)];

@ccclass('Pipe')
export class Pipe extends Barrier {
    @property(Node)
    Cover: Node = null;//覆盖物

    @property(Label3D)
    Label3Ds: Label3D[] = [];

    @property(Node)
    Paths: Node[] = [];

    @property(Node)
    Destination: Node = null;//终点

    @property
    Speed: number = 100;//移动速度

    @property(Collider)
    Collider1: Collider = null;

    @property(Collider)
    Collider2: Collider = null;

    @property(Collider)
    Collider3: Collider = null;

    @property(Collider)
    Collider4: Collider = null;

    TargetParent: Node = null;
    Targets: Node[] = [];//靶子
    Index: number = 0;
    IsCollision: boolean = false;//是否已经发生碰撞
    Colliders: Collider[] = [];
    IsRemove: boolean = false;

    protected onLoad(): void {
        this.TargetParent = this.node.getChildByName("靶子");
        this.TargetParent.children.forEach(e => {
            if (e) {
                e.active = false;
                this.Targets.push(e);
            }
        })

        GameManager.Instance.Pipes.push(this);

        this.Collider1.on(`onTriggerStay`, this.onTriggerStay1, this);
        this.Collider2.on(`onTriggerStay`, this.onTriggerStay2, this);
        this.Collider3.on(`onTriggerStay`, this.onTriggerStay3, this);
        this.Collider4.on(`onTriggerStay`, this.onTriggerStay4, this);
    }

    protected start(): void {
        this.Colliders = [this.Collider1, this.Collider2, this.Collider3, this.Collider4];
        // this.schedule(this.addTarget, 1);
    }

    addTarget() {
        this.decreaseScale();
        if (this.Index < this.Targets.length) {
            this.Targets[this.Index].active = true;
            this.Index++;
        }
    }

    decreaseScale() {
        const curScale = this.Cover.scale.clone().add3f(decrease, 0, 0);
        if (curScale.x < plan[2]) {
            this.release(plan.length - 1);
            return;
        }
        tween(this.Cover)
            .to(0.1, { scale: curScale }, { easing: `bounceOutIn` })
            .call(() => {
                for (let i = plan.length - 1; i >= 0; i--) {
                    if (curScale.x <= plan[i]) {
                        this.release(i);
                        return;
                    }
                }
            })
            .start();
    }

    release(index: number) {
        for (let i = 0; i <= index; i++) {
            this.Label3Ds[i].Color = green;
            this.Colliders[i].node.active = true;
        }
    }

    pushTarget(target: Node) {
        this.move(target);
    }

    move(target: Node) {
        const nextPath: Node = this.Paths[0];
        const targetPos: Vec3 = target.getWorldPosition().clone();
        tween(target)
            .parallel(
                tween(target).to(0.8, { eulerAngles: v3(-720, 0, 0) }).start(),
                tween(target).by(0.8, { worldPosition: v3(0, 0, 0 - 0.028) }).start(),
                tween(target).by(0.4, { worldPosition: v3(0, 0.008, 0) }).by(0.4, { worldPosition: v3(0, -0.008, 0) }).start()
            )
            .to(0.2, { worldPosition: v3(nextPath.getWorldPosition().clone().x, nextPath.getWorldPosition().clone().y, targetPos.z) })
            .call(() => {
                this.moveNextPath(target, 0)
            })
            .start();
    }

    moveNextPath(target: Node, index: number) {
        if (index > this.Paths.length - 1) {
            this.addTarget();
            target.destroy();
            return;
        }
        const nextPath: Node = this.Paths[index];
        const dis: number = GameUtil.getDistanceByNode(target, nextPath);
        tween(target)
            .to(dis / this.Speed, { worldPosition: nextPath.getWorldPosition() })
            .call(() => {
                if (index == 1) {
                    target.eulerAngles = angle[0];
                } else if (index == 2) {
                    target.eulerAngles = angle[1];
                }
                this.moveNextPath(target, index + 1)
            })
            .start();
    }

    onTriggerStay1(event: ITriggerEvent) {
        if (this.IsCollision || this.IsRemove) return;
        if (event.otherCollider.getGroup() == Group.Weapon) {
            //武器升级
            this.IsCollision = true;
            GameManager.Instance.amplifies(1);
        }
    }

    onTriggerStay2(event: ITriggerEvent) {
        if (this.IsCollision || this.IsRemove) return;
        if (event.otherCollider.getGroup() == Group.Weapon) {
            //武器升级
            this.IsCollision = true;
            GameManager.Instance.amplifies(2);
        }
    }

    onTriggerStay3(event: ITriggerEvent) {
        if (this.IsCollision || this.IsRemove) return;
        if (event.otherCollider.getGroup() == Group.Weapon) {
            //武器升级
            this.IsCollision = true;
            GameManager.Instance.amplifies(3);
        }
    }

    onTriggerStay4(event: ITriggerEvent) {
        if (this.IsRemove) return;
        if (event.otherCollider.getGroup() == Group.Bullet) {
            PoolManager.PutNode(event.otherCollider.node);
        } else if (event.otherCollider.getGroup() == Group.Weapon) {
            GameManager.Instance.removePipe();
            this.IsRemove = true;
            this.removeSelf();
        }
    }

    removeSelf() {
        tween(this.node)
            .to(0.1, { scale: v3(1.1, 1.1, 1.1) }, { easing: `quadIn` })
            .to(0.1, { scale: v3(0, 0, 0) }, { easing: `quadOut` })
            .call(() => {
                this.node.destroy();
            })
            .start();
    }
}


